package edu.gatech.fiveminutesleft;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import edu.gatech.fiveminutesleft.R;
import edu.gatech.fiveminutesleft.config.AccountManager;
import edu.gatech.fiveminutesleft.model.Account;

/**
 * Activity which allows user to change their password. Requires them to enter in their old password once
 * and the new password twice.
 * @author g
 *
 */
public class ChangePasswordActivity extends Activity {

	/**
	 * Launches this activity
	 * @param source
	 * @param account
	 */
	public static void launch(Activity source, Account account) {
		Intent n = new Intent(source, ChangePasswordActivity.class);
		GUISpace.put(ChangePasswordActivity.class, "account", account);
		GUISpace.put(ChangePasswordActivity.class, "oldpass", "");
		GUISpace.put(ChangePasswordActivity.class, "newpass", "");
		GUISpace.put(ChangePasswordActivity.class, "confirmpass", "");
		source.startActivity(n);
	}

	private TextView	oldView, newView, confView, statusView;

	@Override
	/**
	 * Sets up GUI instance variables and layout
	 */
	public void onCreate(Bundle b) {
		super.onCreate(b);

		setContentView(R.layout.change_password_layout);

		oldView 	= (TextView) findViewById(R.id.PasswordCurrent);
		newView 	= (TextView) findViewById(R.id.PasswordNew);
		confView 	= (TextView) findViewById(R.id.PasswordConfirm);
		statusView 	= (TextView) findViewById(R.id.PasswordStatus);
	}

	@Override
	/**
	 * Restores saved text field data
	 */
	public void onResume() {
		super.onResume();

		oldView.setText(GUISpace.getString(getClass(), "oldpass"));
		newView.setText(GUISpace.getString(getClass(), "newpass"));
		confView.setText(GUISpace.getString(getClass(), "confirmpass"));
	}

	@Override
	/**
	 * Saves text field data
	 */
	public void onPause() {
		super.onPause();

		GUISpace.put(getClass(), "oldpass", oldView.getEditableText().toString());
		GUISpace.put(getClass(), "newpass", newView.getEditableText().toString());
		GUISpace.put(getClass(), "confirmpass", confView.getEditableText().toString());
	}

	@Override
	/**
	 * Clears the GUISpace variables this Activity was using
	 * if the Activity is finishing (rather than just, say, rotating)
	 */
	public void onDestroy() {
		super.onDestroy();

		if (isFinishing()) {
			GUISpace.clearPrefix(getClass());
		}
	}

	/**
	 * Called when user presses "cancel" button
	 * to close the Activity
	 * @param v
	 */
	public void cancel(View v) {
		finish();
	}

	/**
	 * Called when the user presses the "save" button
	 * to change the user's password (or display an error)
	 * and close the Activity if successful.
	 * @param v
	 */
	public void save(View v) {
		String opass = oldView.getEditableText().toString();
		String npass = newView.getEditableText().toString();
		String confirmPass = confView.getEditableText().toString();

		Account account = (Account) GUISpace.get(getClass(), "account");

		String error = AccountManager.getInstance().changePassword(account, opass, npass, confirmPass);
		if (error != null) {
			statusView.setText(error);
			return;
		}

		finish();
	}
}
